<?php

// [ 实时采集开奖结果 ]
// POST pk10.realCollect
// http://sg.io/api?pk10.realCollect
// http://sg.51hub.cn/api?pk10.realCollect

namespace app\api\resource\pk10;
use think\Db;
use app\common\lib\RealCollect as RealCollectLib;
use app\common\lib\Plan as PlanLib;
use app\common\lib\Loc as LocLib;
use app\common\lib\PlanCallback as PlanCallbackLib;

class RealCollect
{
    // 方法
    public $_method = ['GET','POST'];
    // 前置
    public $_pre    = [];
    // 描述
    public $_description = '实时采集开奖结果';
    // 参数
    public $_param  = [];

    public function run(&$request)
    {
        $res = RealCollectLib::run();
        if (!$res['ret']) return [ 400,$res['msg'] ];
        // p($res);die;

        $res = $res['data'];
        $ret = Db::name('pk10')->where('no',$res['no'])->find();
        if ( !is_null($ret) ) return [201,$ret];
        // p($res);die;

        $data['no']  = $res['no'];
        $data['v1']  = $res['item'][0];
        $data['v2']  = $res['item'][1];
        $data['v3']  = $res['item'][2];
        $data['v4']  = $res['item'][3];
        $data['v5']  = $res['item'][4];
        $data['v6']  = $res['item'][5];
        $data['v7']  = $res['item'][6];
        $data['v8']  = $res['item'][7];
        $data['v9']  = $res['item'][8];
        $data['v10'] = $res['item'][9];
        $data['create_time'] = $res['time'];
        $num = Db::name('pk10')->insert($data);
        // p($num);die;

        if ($num >= 0) {
            // 记录 | 本期位移
            LocLib::run($res['no'],$res['item']);

            // 生成 | 下期计划
            $no = $res['no'] + 1;
            PlanLib::run($no,$res['item'],true);

            // 更新 | 本期计划中奖位
            PlanCallbackLib::run($res['no'],$res['item']);

            return [201,$data];
        } else {
            return 500;
        }
    }
}
